from django.http import JsonResponse


class HttpCode:

    ok = 200 # 正常
    params_error = 400 # 参数格式错误
    unauth_error = 401 # 未授权
    method_error = 405 # 请求方法错误
    server_error = 500 # 服务器错误


def result(code=HttpCode.ok, message="",data=None,kwargs=None):
    json_dict = {'code':code,'message':message,'data':data}

    if kwargs and isinstance(kwargs,dict):
        json_dict.update(kwargs)

    return JsonResponse(json_dict)


def ok():
    return result()


def params_error(message="", data=None):
    return result(code=HttpCode.params_error,message=message,data=data)


def unauth(message="",data=None):
    return result(code=HttpCode.unauth_error,message=message,data=data)


def server_error(message="",data=None):
    return result(code=HttpCode.server_error,message=message,data=data)

